class UserError(Exception):
    """用户相关错误的基类"""
    def __init__(self, message: str, code: int = 400):
        self.message = message
        self.code = code
        super().__init__(self.message)

class UserNotFoundError(UserError):
    """用户不存在错误"""
    def __init__(self, message: str = "用户不存在"):
        super().__init__(message, 404)

class UserAlreadyExistsError(UserError):
    """用户已存在错误"""
    def __init__(self, message: str = "用户已存在"):
        super().__init__(message, 409)

class InvalidCredentialsError(UserError):
    """无效的凭证错误"""
    def __init__(self, message: str = "用户名或密码错误"):
        super().__init__(message, 401)

class AccountDeactivatedError(UserError):
    """账户已停用错误"""
    def __init__(self, message: str = "账户已被停用"):
        super().__init__(message, 403)

class ValidationError(UserError):
    """数据验证错误"""
    def __init__(self, message: str = "数据验证失败"):
        super().__init__(message, 400)

def handle_user_error(error: UserError):
    """
    统一处理用户相关错误
    返回标准化的错误响应
    """
    from flask import jsonify
    response = {
        "success": False,
        "message": error.message,
        "error_type": error.__class__.__name__
    }
    return jsonify(response), error.code

